package com.aquarium.mail;

import com.aquarium.util.SPUtils;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

public class MailInfo {
	private String TAG = "MailInfo";
	private static String MAILSETTING_XML = "MAILSETTING_XML";
	private static String MAIL_SERVER_HOST_KEY = "MAIL_SERVER_HOST_KEY";
	private static String MAIL_SERVER_PORT_KEY = "MAIL_SERVER_PORT_KEY";
	private static String FROM_ADDR_KEY = "FROM_ADDR_KEY";
	private static String TO_ADDR_KEY = "TO_ADDR_KEY";
	private static String USER_NAME_KEY = "USER_NAME_KEY";
	private static String PASS_WORD_KEY = "PASS_WORD_KEY";
	private static String VALIDATE_KEY = "VALIDATE_KEY";
	private static String MAIL_ENABLE_KEY = "MAIL_ENABLE_KEY";
	private static String IS_SSL_KEY = "IS_SSL_KEY";

	private String mailServerHost;
	private String mailServerPort;
	// 邮件接收者的地址
	private String toAddress;
	// 登陆邮件发送服务器的用户名和密码
	private static String userName;
	private static String passWord;
	// 是否需要身份验证
	private String isSSL;
	public static boolean validate = true;
	// 邮件主题
	private String subject = "aquarium";
	// 邮件的文本内容
	private String content = "mail content ,hello word";

	private static MailInfo mMailInfo;
	private static Context mContext;
	private static SharedPreferences mSharedPreferences;
	private static SharedPreferences.Editor mEditor;

	private MailInfo(Context context) {
		mContext = context;
		mSharedPreferences = mContext.getSharedPreferences(MAILSETTING_XML,
				Context.MODE_PRIVATE);
		mEditor = mSharedPreferences.edit();
		intitAllValue();
	}

	public static MailInfo getInstance(Context context) {
		if (mMailInfo == null) {
			mMailInfo = new MailInfo(context);
		}
		return mMailInfo;
	}

	private void intitAllValue() {
		mailServerHost = getMailServerHost();
		mailServerPort = getMailServerPort();
		toAddress = getToAddress();
		userName = getUserName();
		passWord = getPassword();
		subject = getSubject();
		content = getContent();
		isSSL = getIsSSL();
	}

	public void setIsSSL(String value) {
		this.isSSL = value;
		SPUtils.setSP(mEditor, IS_SSL_KEY, value);
		Log.d(TAG, "setIsSSL"+isSSL);
		if (isSSL==null) {
			Log.d(TAG, "isssl is null");
			isSSL="false";
		}
	}

	public String getIsSSL() {
		if (isSSL == null) {
			isSSL = SPUtils.getSP(mSharedPreferences, IS_SSL_KEY, "false");
			if (isSSL==null) {
				Log.d(TAG, "isssl is null");
				isSSL="false";
			}
		}
		return isSSL;
	}

	public void setMailServerHost(String mailServerHost) {
		this.mailServerHost = mailServerHost;
		SPUtils.setSP(mEditor, MAIL_SERVER_HOST_KEY, mailServerHost);
	}

	public String getMailServerHost() {
		if (mailServerHost == null) {
			mailServerHost = SPUtils.getSP(mSharedPreferences,
					MAIL_SERVER_HOST_KEY, "null");
		}
		return mailServerHost;
	}

	public void setMailServerPort(String mailServerPort) {
		this.mailServerPort = mailServerPort;
		SPUtils.setSP(mEditor, MAIL_SERVER_PORT_KEY, mailServerPort);
	}

	public String getMailServerPort() {
		if (mailServerPort == null) {
			mailServerPort = SPUtils.getSP(mSharedPreferences,
					MAIL_SERVER_PORT_KEY, " null");
		}
		return mailServerPort;
	}

	public void setToAddress(String toAddress) {
		this.toAddress = toAddress;
		SPUtils.setSP(mEditor, TO_ADDR_KEY, toAddress);
	}

	public String getToAddress() {
		if (toAddress == null) {
			toAddress = SPUtils.getSP(mSharedPreferences, TO_ADDR_KEY, "null");
		}
		return toAddress;
	}

	public String getUserName() {
		if (userName == null) {
			userName = SPUtils.getSP(mSharedPreferences, USER_NAME_KEY, "null");
		}
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
		SPUtils.setSP(mEditor, USER_NAME_KEY, userName);
	}

	public String getPassword() {
		if (passWord == null) {
			passWord = SPUtils.getSP(mSharedPreferences, PASS_WORD_KEY, "null");
		}
		return passWord;
	}

	public void setPassword(String password) {
		this.passWord = password;
		SPUtils.setSP(mEditor, PASS_WORD_KEY, password);

	}

	public String getSubject() {
		return "subject";
	}

	public String getContent() {
		return "content";
	}

	public boolean isEmailEnable() {
		return SPUtils.getSP(mSharedPreferences, MAIL_ENABLE_KEY, false);

	}

	public void setEmailEnable(boolean value) {
		SPUtils.setSP(mEditor, MAIL_ENABLE_KEY, value);
	}

}
